using System;
using System.Xml;

namespace umbracoUtilities.Search
{
	/// <summary>
	/// Summary description for Settings.
	/// </summary>
	public class Settings
	{

		private static bool _initialized = false;

		// Key to use when splitting paths
		public static string PathSplit = "s";

		// Directory where indexed data should be stored
		public static string IndexDirectory = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.StorageDirectory + "/umbSearch");

		// Index the following data aliases (separate with semicolon (;))
		private static string _indexDataWithAliases = "bodyText;content";

		// Do not index pages with umbracoNaviHide attribute
		private static bool _excludeUmbracoNaviHide = true;

		// Do not index nodes with following nodeType Aliases (separate with semicolon (;))
		private static string _excludeNodeTypes = "";

		// Do not index pages under following id's (separate with semicolon(;))
		private static string _excludeIds = "";

		public static string _configSourcePath = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.StorageDirectory + "/umbracoSearchConfig.xml");
		
		private static XmlDocument _configSource;

		public static string IndexDataWithAliases 
		{
			get 
			{
				if (!_initialized)
					initializeSettings();
				return _indexDataWithAliases;
			}
		}
		public static bool ExcludeUmbracoNaviHide 
		{
			get 
			{
				if (!_initialized)
					initializeSettings();
				return _excludeUmbracoNaviHide;
			}
		}
		public static string ExcludeNodeTypes 
		{
			get 
			{
				if (!_initialized)
					initializeSettings();
				return _excludeNodeTypes;
			}
		}
		public static string ExcludeIds 
		{
			get 
			{
				if (!_initialized)
					initializeSettings();
				return _excludeIds;
			}
		}

		public Settings()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public static XmlDocument Source 
		{
			get 
			{
				if (_configSource == null) 
					Reload();

				return _configSource;
			}
		}

		public static void Reload() 
		{
			if (_configSource == null)
				_configSource = new XmlDocument();
			_configSource.Load(_configSourcePath);
		}

		private static void initializeSettings() 
		{
			if (!_initialized) 
			{
				XmlDocument n = Source;
                
				// Get config items
				_excludeIds = umbraco.xmlHelper.GetNodeValue(n.DocumentElement.SelectSingleNode("/indexConfiguration/excludeIds"));
				_excludeNodeTypes = umbraco.xmlHelper.GetNodeValue(n.DocumentElement.SelectSingleNode("/indexConfiguration/excludeNodeTypes"));
				_excludeUmbracoNaviHide = bool.Parse(umbraco.xmlHelper.GetNodeValue(n.DocumentElement.SelectSingleNode("/indexConfiguration/excludeUmbracoNaviHide")));
				_indexDataWithAliases = umbraco.xmlHelper.GetNodeValue(n.DocumentElement.SelectSingleNode("/indexConfiguration/indexDataWithAliases"));
				_initialized = true;
			}
		}
	}
}
